<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <div class="layui-btn-container"></div>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script type="application/javascript" src="../js/jquery-1.8.3.min.js"></script>
    <script type="application/javascript" src="../layui/layui.js"></script>
    <script type="application/javascript" src="../layui/layui.all.js"></script>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">登录名</label>
        <div class="layui-input-block">
            <input type="text" name="loginname" id="loginname" lay-verify="title" autocomplete="off"
                   placeholder="请输入登录名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" id="password" lay-verify="pass" placeholder="请输入密码"
                   autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="username" lay-verify="required" lay-reqtext="用户名是必填项，岂能为空？"
                   placeholder="请输入用户名"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">用户状态</label>
        <div class="layui-input-block">
            <input type="text" name="status" id="status" lay-verify="title1" autocomplete="off" placeholder="请输入用户状态"
                   class="layui-input">
        </div>
    </div>

    <!--<div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">日期</label>
            <div class="layui-input-inline">
                <input type="text" name="date" id="createDate" lay-verify="date" placeholder="yyyy-MM-dd"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>-->


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>

<script>
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            layedit = layui.layedit,
            laydate = layui.laydate;

        //监听提交
        form.on('submit(demo1)', function (data) {
            $.ajax({
                url: 'addUser.do',
                data: data.field,
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        console.log(data);
                        layer.alert(data.data);
                        location.reload(); //添加成功后再刷新
                    } else {
                        layer.alert("添加失败");
                    }
                }
            });
            return false;
        });

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '登录名不行啊';
                }
            },
            title1: function (value) {
                if (value.length > 1) {
                    return '用户状态输入一个数字就得了';
                }
            },
            pass: [
                /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
            ],
            content: function (value) {
                layedit.sync(editIndex);
            }
        });


        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });


        //监听提交
        /*form.on('submit(demo1)', function(data) {
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            })
            return false;
        });*/


        //表单取值
        /*layui.$('#LAY-component-form-getval').on('click', function() {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });*/

    });
</script>
<!--<script>
    $("form").submit(function (data) {
        $.ajax({
            type: "post",
            url: "addUser.do",
            data: data.field,
            dataType: "json",
            success: function (data) {
                //data就是后端返回的数据 {"code":1, "info":null}
                if (data.code == 1) {
                    alert("添加成功！");
                    window.location.href = "showUser.html"
                } else {
                    alert(data.info)
                }
            }
        })
    })
</script>-->
</html>
